/**
 * Created by admin on 2016/7/25.
 */
/*init of chart*/
var myChart = echarts.init(document.getElementById('chartBox'));

// 指定图表的配置项和数据
var dataBloodPressure = {
    'heartRate': [55, 20, 36, 10, 10],
    'highPressure': [152, 145, 146, 175, 160],
    'lowPressure': [5, 45, 88, 45, 10, 65],
    'list': ['心率', '高压', '低压'],
    'timeX': ["05", "05", "06", "07", "08", "08", "08", "09", "10", "12"],
    'name': '血压 | 心率',
    'max': 180,
    'min': 0,
    'split': 15,
    'interval': 15 ,
    'type': 'bloodPressure'
};
var dataBloodSugar = {
    'bloodSugar': [5.6, 7.2, 6.6, 4.3, 6.5, 8.8],
    'list': ['血糖'],
    'timeX': ["05", "05", "06", "07", "08", "08", "08", "09", "10", "12"],
    'name': '血糖',
    'max': 22,
    'min': 0,
    'split': 2,
    'interval':2,
    'type': 'bloodSugar'
};
var dataBMI = {
    'BMI': [19, 20, 18.6, 22, 23, 25],
    'timeX': ["05", "05", "06", "07", "08", "10", "12"],
    'type': 'bmi',
    'name': 'BMI',
    'max': 36,
    'min': 0,
    'split': 3,
    'interval':3,
    'list': ['BMI']
};

var option = function (data) {
    return {
        title: {
            show: false
        },
        grid: {
            show: false,
            containerLabel: true,
        },
        tooltip: {},
        legend: {
            bottom: 10,
            itemWidth: 20,
            itemHeight: 10,
            data: data.list
        },
        xAxis: {
            axisLine: {
                lineStyle: {
                    color: "#CFCFCF"
                }
            },
            axisTick: {
                alignWithLabel: true
            },

            interval: 5,
            name: "时间",
            nameGap:0,
            data: data.timeX
        },
        yAxis: {
            axisLine: {
                lineStyle: {
                    color: "#CFCFCF"
                }
            },
            name: data.name,
            min: data.min,
            max: data.max,
            splitNumber: data.split,
            interval: data.interval,
            splitLine: {
                show: false
            }
        },
        textStyle: {
            fontSize: 12,
            color: "#999999",
            fontFamily: 'Helvetica, Microsoft Yahei, Hiragino Sans GB, WenQuanYi Micro Hei, sans-serif',
        },
        color: ['#F9D8DB', '#61C8F7', '#70BF73'],
        series: getSeriesArray(data)
    }
};

function getSeriesArray(data) {
    switch (data.type) {
        case 'bloodPressure':
            return [{
                    name: '心率',
                    type: 'bar',
                    data: data.heartRate
            },
                {
                    name: '高压',
                    type: 'line',
                    data: data.highPressure
                },
                {
                    name: '低压',
                    type: 'line',
                    data: data.lowPressure
                }];
            break;
        case 'bloodSugar':
            return [
                {
                    name: '血糖',
                    type: 'line',
                    itemStyle:{
                        normal:{ color:'#61C8F7'}
                    },
                    data: data.bloodSugar
                }];
            break;
        case  'bmi':
            return [
                {
                    name: 'BMI',
                    type: 'line',
                    itemStyle:{
                        normal:{ color:'#61C8F7'}
                    },
                    data: data.BMI
                }];
            break;
    }
}

// 使用刚指定的配置项和数据显示图表。
function drawChart(data) {
    myChart.setOption(option(data));
}
drawChart(dataBloodSugar);
